package com.dfbz.bigdata.modules.system.shop.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dfbz.bigdata.common.utils.R;

import com.dfbz.bigdata.modules.system.shop.order.doman.UanddExpress;
import com.dfbz.bigdata.modules.system.shop.order.doman.UandubOrders;
import com.dfbz.bigdata.modules.system.shop.order.dto.MiniUandubOrdersDto;
import com.dfbz.bigdata.modules.system.shop.order.dto.UandubOrdersDto;
import com.dfbz.bigdata.modules.system.shop.order.services.UanddExpressServiceMini;
import com.dfbz.bigdata.modules.system.shop.order.services.UandubOrdersServiceMini;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Auther:东方标准
 * @Date:2022/9/25-09-25-17:35
 * @Decsription:com.dfbz.bigdata.modules.system.controller
 * @Version:1.0
 */
@RestController
@RequestMapping("/uuborder")
@Slf4j
public class UandubOrdersControllerMini {

    @Autowired
    private UandubOrdersServiceMini uandubOrdersService;

    @Autowired
    private UanddExpressServiceMini uanddExpressServiceMini;


    //    查询订单分页
    @GetMapping
    public R<List<UandubOrdersDto>> getOrderList(String uubOdNum, int currentPage, int pagesizes) {

        log.info("udOdNum------->{}", uubOdNum);
        List<UandubOrdersDto> uandubOrderList = uandubOrdersService.getUandubOrderList(uubOdNum, currentPage, pagesizes);

        for (UandubOrdersDto uandubOrdersDto : uandubOrderList) {
            uandubOrdersDto.setUserMsg("用户姓名: " + uandubOrdersDto.getUserName() + ",手机号: " + uandubOrdersDto.getUserPhone());
            uandubOrdersDto.setBrandMsg("品牌店铺名称: " + uandubOrdersDto.getBrandName() +
                    ",店铺描述: " + uandubOrdersDto.getBrandText() +
                    ",店铺等级: " + uandubOrdersDto.getBrandLable());
        }


        return R.success(uandubOrderList);
    }
    //根据用户id查询订单
    @GetMapping("/get")
    public R<List<MiniUandubOrdersDto>> getOrderUandBList(Long userId,Long expressStatus){
        System.out.println(userId);

        List<MiniUandubOrdersDto> miniOrderList = uandubOrdersService.getMiniOrderList(userId,expressStatus);
        return R.success(miniOrderList);

    }

    @GetMapping("/total")
    public R<Integer> getDicTotal() {

        return R.success(uandubOrdersService.getTotal());

    }


//    @GetMapping
//    public R<List<UandubOrdersDto>> getorderlist(String uubOdNum){
//        System.out.println(uubOdNum+"------");
//
//        LambdaQueryWrapper<UandubOrders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.like(!uubOdNum.isEmpty(),UandubOrders::getUubOdNum,uubOdNum);
//        List<UandubOrders> uandubOrders = uandubOrdersService.list(lambdaQueryWrapper);
//
//        List<UandubOrdersDto> uandubOrdersDtos=new ArrayList<>();
//
//        for (UandubOrders orders:uandubOrders){
//            UandubOrdersDto uandubOrdersDto=new UandubOrdersDto();
////            得到下单用户id  商品id  商家id
//            Long userId = orders.getUserId();
//            Long goodId = orders.getGoodId();
//            Long brandId = orders.getUubOdBusinessId();
//
////            查询到下单用户的信息
//            R<UserDesignerandpersonal> userR = userDesignerApi.findById(userId);
////            查询商品信息
//            R<CommonGoods> goodsR = userDesignerApi.findGoodsById(goodId);
//            //查询商家信息
//            R<UserBrand> brandR=userDesignerApi.findBrandById(brandId);
////            order对象中重复属性copy到dto
//            BeanUtils.copyProperties(orders,uandubOrdersDto);
//            //            用户信息赋值到dto
//            UserDesignerandpersonal user = userR.getData();
//            String userMsg="姓名:"+user.getUserName()+",手机号:"+user.getUserPhone();
//            uandubOrdersDto.setUserMsg(userMsg);
//            //商品信息给dto
//            CommonGoods commonGoods=goodsR.getData();
//            if(commonGoods!=null) {
//                String goodsMsg = "商品名:" + commonGoods.getGoodName() + ",商品价格:" + commonGoods.getGoodPrice() +
//                        ",商品描述:" + commonGoods.getGoodDescription();
//                uandubOrdersDto.setGoodMsg(goodsMsg);
//            }
//            //商家信息给dto
//            UserBrand userBrand=brandR.getData();
////            String brandMsg="商家名:"+userBrand.getBrandsName()+",等级标签:"+commonGoods.getIsDeleted();
////            uandubOrdersDto.setBrandMsg(brandMsg);
//
//            uandubOrdersDtos.add(uandubOrdersDto);
//      }
//        return R.success(uandubOrdersDtos);
//
//
//    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Long id) {
        boolean b = uandubOrdersService.removeById(id);
        if (b) {
            return R.success("删除成功");
        } else return R.error("系统繁忙");

    }

    @GetMapping("/orderId")
    public R<UandubOrders> getOrderId(Long orderId) {
        LambdaQueryWrapper<UandubOrders> lqw=new LambdaQueryWrapper<>();
        lqw.eq(UandubOrders::getUubOdId, orderId);
        UandubOrders OrderOne = uandubOrdersService.getOne(lqw);
        return R.success(OrderOne);
    }

    @PostMapping("/save")
    public R<String> saveOrder(@RequestBody UandubOrders uandubOrders){
        log.info("----------------{}",uandubOrders);
//        return R.success("text");
        uandubOrders.setCreateTime(new Date());
        uandubOrders.setUpdateTime(new Date());
        uandubOrders.setUubOdCreatetime(new Date());
        uandubOrders.setUubOdCheckouttime(new Date());
        String uuid = UUID.randomUUID().toString();
        String regEx="[^0-9]";
        Pattern p= Pattern.compile(regEx);
        Matcher m=p.matcher(uuid);
        String UubOdNum = m.replaceAll("").trim();
        uandubOrders.setUubOdNum(UubOdNum);
        uandubOrders.setSafeId(1L);
        uandubOrders.setUubExpressId(2L);
        uandubOrdersService.save(uandubOrders);

        return R.success("新增成功");
    }

    @PostMapping("/saveyzf")
    public R<String> saveOrderyzf(@RequestBody UandubOrders uandubOrders){
        log.info("----------------{}",uandubOrders);
//        return R.success("text");
        uandubOrders.setCreateTime(new Date());
        uandubOrders.setUpdateTime(new Date());
        uandubOrders.setUubOdCreatetime(new Date());
        uandubOrders.setUubOdCheckouttime(new Date());
        String uuid = UUID.randomUUID().toString();
        String regEx="[^0-9]";
        Pattern p= Pattern.compile(regEx);
        Matcher m=p.matcher(uuid);
        String UubOdNum = m.replaceAll("").trim();
        uandubOrders.setUubOdNum(UubOdNum);
        uandubOrders.setSafeId(1L);

        long time = new Date().getTime();
        //生成物流信息
        UanddExpress uanddExpress=new UanddExpress();
        uanddExpress.setExpressId(time+10000000);
        uanddExpress.setOdId(UubOdNum);
        uanddExpress.setExpressNumber(UUID.randomUUID().toString());
        uanddExpress.setExpressStatus(2);
        uanddExpress.setDeliveryAdd("湖北省武汉市武昌区友谊大道18号,武昌小东门市政建材市场");
        uanddExpress.setTakeAdd("默认");
        uanddExpress.setCreateTime(new Date());
        uanddExpress.setUpdateTime(new Date());
        uanddExpressServiceMini.save(uanddExpress);

        uandubOrders.setUubExpressId(time+10000000);
        uandubOrdersService.save(uandubOrders);

        return R.success("新增成功");
    }

    @GetMapping("{id}")
    public R<String> getExpresspost(@PathVariable Long id){
        LambdaQueryWrapper<UandubOrders> lqw=new LambdaQueryWrapper<>();
        lqw.eq(UandubOrders::getUubOdId,id);
        UandubOrders one = uandubOrdersService.getOne(lqw);
        UanddExpress express = uanddExpressServiceMini.getById(one.getUubExpressId());
        express.setExpressStatus(3);
        uanddExpressServiceMini.updateById(express);
        return R.success("更新成功");
    }
}
